<?php
// 获取报修信息
header("content-type:text/html;charset=utf-8");
include_once("conn.php");
if(!session_id()){
    session_start();
}
//条件查询历史报修信息
$uid = $_SESSION['USER']['u_id'];
if(!empty($_GET['action'])){
    if($_GET['action'] == 'searchbyid'){
        $info = $_GET['fix_id'];
        $sql = "SELECT * FROM tb_fixinfo_request WHERE fix_status != '0' AND fix_status != '1' AND fix_id LIKE '%".$info."%'  AND u_id = '". $uid."' ORDER BY fix_sendtime DESC";
    }
    if($_GET['action'] == 'searchbytime'){
        $start = $_GET['start'];
        $end = $_GET['end'];
        $sql = "SELECT * FROM tb_fixinfo_request WHERE fix_status != '0' AND fix_status != '1' AND fix_sendtime BETWEEN '". $start ."' AND '". $end ."' AND u_id = '". $uid."' ORDER BY fix_sendtime DESC";
        // echo $sql;
    }
    $result = mysqli_query($conn_DB, $sql);
    if ($result) {
        echo"
            <thead>
                <tr>
                    <th scope='col'>报修单号</th>
                    <th scope='col'>故障地点</th>
                    <th scope='col'>故障类型</th>
                    <th scope='col'>报修时间</th>
                    <th scope='col'>当前状态</th>
                    <th scope='col'>结束时间</th>
                    <th scope='col'>操作</th>
                </tr>
            </thead>
        ";
        if (mysqli_num_rows($result) == 0) {
            echo "<tbody><tr><td colspan='7'>暂无数据</td></tr></tbody>";
        } else {
            echo"<tbody>";
            while ($row = mysqli_fetch_assoc($result)) {
                if ($row['fix_status'] == 0) {
                    $status = "等待受理";
                } else if ($row['fix_status'] == 1) {
                    $status = "已受理，等待维修";
                } else if ($row['fix_status'] == 2) {
                    $status = "维修完成";
                } else{
                    $status = "撤销申请";
                }
                echo "
                    <tr>
                        <th scope='row'>". $row['fix_id'] ."</th>
                        <td>". $row['fix_area'] ."</td>
                        <td>". $row['fix_type'] ."</td>
                        <td>". $row['fix_sendtime'] ."</td>
                        <td>". $status ."</td>
                        <td>". $row['fix_endtime'] ."</td>
                        <td><a href='u-showfixinfo.php?fix_id=". $row['fix_id'] ."'>查看详情</a></td>
                    </tr>
                ";
            }
            echo"</tbody>";
        }
    }
}

//获取正常处理的报修信息
function get_follow_info()
{
    global $conn_DB;
    global $uid;
    $sql = "SELECT * FROM tb_fixinfo_request WHERE fix_status != '2' AND fix_status != '-1' AND u_id = '". $uid."' ORDER BY fix_sendtime DESC";
    $result = mysqli_query($conn_DB, $sql);
    if (!$result) {
        die("<script>alert('获取报修信息失败！请重试！');location='u-main.php';</script> ");
    }
    if (mysqli_num_rows($result) == 0) {
        echo "<div class='main-div-followlist-content'>
                    <div class='main-div-followlist-content-item'>
                        <h4><i class='fas fa-info-circle' style='color: #0d6efd; font-size: 22px;'></i> 报修信息</h4>
                        <hr>
                        <div class='none-info' style='text-align: center;'>
                            暂无报修信息
                        </div>
                    </div>
                </div>";
    } else {
        while ($row = mysqli_fetch_assoc($result)) {
            if ($row['fix_status'] == 0) {
                $status = "等待受理";
            } else if ($row['fix_status'] == 1) {
                $status = "已受理，等待维修";
            }
            echo "
                    <div class='main-div-followlist-content'>
                        <div class='main-div-followlist-content-item'>
                            <h4><i class='fas fa-info-circle' style='color: #0d6efd; font-size: 22px;'></i> 报修信息</h4>
                            <hr>
                            <div class='main-div-followlist-content-item-line'>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>报修单号：</span>
                                    <span class='line-items-info'>" . $row['fix_id'] . "</span>
                                </div>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>报修人：</span>
                                    <span class='line-items-info'>" . $row['fix_name'] . "</span>
                                </div>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>联系电话：</span>
                                    <span class='line-items-info'>" . $row['fix_phone'] . "</span>
                                </div>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>故障地点：</span>
                                    <span class='line-items-info'>" . $row['fix_area'] . "</span>
                                </div>
                            </div>
                            <div class='main-div-followlist-content-item-line'>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>详细地址：</span>
                                    <span class='line-items-info'>" . $row['fix_address'] . "</span>
                                </div>
                            </div>
                            <div class='main-div-followlist-content-item-line'>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>故障类型：</span>
                                    <span class='line-items-info'>" . $row['fix_type'] . "</span>
                                </div>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>故障原因：</span>
                                    <span class='line-items-info'>" . $row['fix_reason'] . "</span>
                                </div>
                            </div>
                            <div class='main-div-followlist-content-item-line'>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>提交时间：</span>
                                    <span class='line-items-info'>" . $row['fix_sendtime'] . "</span>
                                </div>
                            </div>
                            <div class='main-div-followlist-content-item-line'>
                                <div class='content-item-line-items'>
                                    <span class='line-items-title'>故障描述：</span>
                                    <span class='line-items-info'>" . $row['fix_depicted'] . "</span>
                                </div>
                            </div>
                            <hr style='border: 1px dashed #ccc;'>
                            <div class='main-div-followlist-state'>
                                <h4><i class='fas fa-wrench' style='color: #0d6efd; font-size: 22px;'></i> 跟进情况</h4>
                                <hr>
                                <div class='main-div-followlist-content-item-line'>
                                    <div class='content-item-line-items'>
                                        <span class='line-items-title'>当前状态：</span>
                                        <span class='line-items-info'>" . $status . "</span>
                                    </div>
                                    <a href='u-showfixinfo.php?fix_id=" . $row['fix_id'] . "' title='点我查看详细信息'>
                                        <span style='color: #0d6efd;'>点此查看详情</span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
            ";
        }
    }
}

//获取可撤销的报修信息
function get_retract_info()
{
    global $conn_DB;
    global $uid;
    $sql = "SELECT * FROM tb_fixinfo_request WHERE fix_status = '0' AND u_id = '". $uid."' ORDER BY fix_sendtime DESC";
    $result = mysqli_query($conn_DB, $sql);
    if (!$result) {
        die("<script>alert('获取报修信息失败！请重试！');location='u-main.php';</script> ");
    }
    if (mysqli_num_rows($result) == 0) {
        echo "
            <div class='none-info'>
                暂无可撤回的报修申请
            </div>
        ";
    } else {
        echo "
            <table class='table table-hover'>
                <thead>
                    <tr>
                        <th scope='col'>报修单号</th>
                        <th scope='col'>报修人</th>
                        <th scope='col'>联系电话</th>
                        <th scope='col'>故障类型</th>
                        <th scope='col'>提交时间</th>
                        <th scope='col'>操作</th>
                    </tr>
                </thead>
                <tbody>
        ";
        while ($row = mysqli_fetch_assoc($result)) {
            if ($row['fix_status'] != 0) {
                continue;
            }
            echo "
                <tr>
                    <th scope='row'>" . $row['fix_id'] . "</th>
                    <td>" . $row['fix_name'] . "</td>
                    <td>" . $row['fix_phone'] . "</td>
                    <td>" . $row['fix_type'] . "</td>
                    <td>" . $row['fix_sendtime'] . "</td>
                    <td>
                        <a href='php/action-deal.php?fix_id=" . $row['fix_id'] . "&action=retract'>
                            <button type='button' class='btn btn-outline-danger'>撤销</button>
                        </a>
                    </td>
                </tr>
            ";
        }
        echo "</tbody></table>";
    }
}

//获取一条报修id的所有信息
function get_show_info($fix_id)
{
    global $conn_DB;
    global $uid;
    $sql = "SELECT * FROM tb_fixinfo_request WHERE fix_id = '" . $fix_id . "' AND u_id = '" . $_SESSION['USER']['u_id'] . "'";
    $result = mysqli_query($conn_DB, $sql);
    if (!$result) {
        die("<script>alert('获取报修信息失败！请重试！');location='u-main.php';</script> ");
    }
    if (mysqli_num_rows($result) == 0) {
        die("<script>alert('该条信息不存在！');location='u-main.php';</script> ");
        // echo $sql;
    }
    $row = mysqli_fetch_assoc($result);
    if ($row['fix_status'] == 0) {
        $status = "等待受理";
    } else if ($row['fix_status'] == 1) {
        $status = "已受理，等待维修";
    } else if ($row['fix_status'] == 2) {
        $status = "维修完成";
    } else {
        $status = "申请取消";
    }
    $i = '';
    if($row['fix_status'] == 1 || $row['fix_status'] == 2){
        $sql_1 = "SELECT *
            FROM tb_fix_repairer
            LEFT JOIN tb_repairer ON tb_fix_repairer.r_id = tb_repairer.r_id
            WHERE tb_fix_repairer. fix_id = '" . $fix_id . "'";
            // echo $sql_1;

        if (!$result_1 = mysqli_query($conn_DB, $sql_1)) {
            die("<script>alert('获取报修信息失败！请重试！');location='u-main.php';</script> ");
        }else{
            $row_1 = mysqli_fetch_assoc($result_1);
            $i = "
            <div class='info-div'>
                <div class='info-div-tope'>
                    维修人姓名：
                </div>
                <div class='info-div-bottom'>
                    " . $row_1['r_name'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    维修人工号：
                </div>
                <div class='info-div-bottom'>
                    " . $row_1['r_id'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    维修人联系电话：
                </div>
                <div class='info-div-bottom'>
                    " . $row_1['r_phone'] . "
                </div>
            </div>
            ";
        }
    }

    if($row['fix_endtime'] == null){
        $endtime = "未完成";
    }
    else{
        $endtime = $row['fix_endtime'];
    }
    echo "
        <div class='fixinfo'>
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修单号：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_id'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修人：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_name'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    联系电话：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_phone'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    故障发生区域：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_area'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    故障详细地址：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_address'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    故障类型：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_type'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    故障原因：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_reason'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    故障描述：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_depicted'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修图片：
                </div>
                <div class='info-div-img'>
                    <img src='../img/user_upload/" . $row['fix_img'] . "' alt='图片找不到了QAQ'>
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修申请日期：
                </div>
                <div class='info-div-bottom'>
                    " . $row['fix_sendtime'] . "
                </div>
            </div>
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修状态：
                </div>
                <div class='info-div-bottom'>
                    " . $status . "
                </div>
            </div>
            ".$i."
            <div class='info-div'>
                <div class='info-div-tope'>
                    报修完成日期：
                </div>
                <div class='info-div-bottom'  style='margin-bottom: 10px;'>
                    " . $endtime . "
                </div>
            </div>
        </div>";
    if ($row['fix_status'] == 2) {
        $sql_feedback = "SELECT * FROM tb_feedback WHERE fix_id = '" . $row['fix_id']. "'";
        $result_feedback = mysqli_query($conn_DB, $sql_feedback);
        if ($result_feedback) {
            echo"
                <div class='feedback-div'>
                    <h4>反馈信息</h4>
                    <hr class='hr-feedback'>
                    <div class='feedback-div-items'>
                        <span style='font-weight: bold;'>反馈状态：</span>
            ";
            if (mysqli_num_rows($result_feedback) == 0) {
                echo"<span>未反馈</span></div>
                    <div class='feedback-form-div'>
                        <span style='font-weight: bold;'>填写反馈信息：</span>
                        <br>
                        <form class='feedback-form' method='post' action='php/send-feedback.php'>
                            <input type='hidden' name='fix_id' value='" . $row['fix_id']. "'>
                            <input type='hidden' name='u_id' value='" . $_SESSION['USER']['u_id']. "'>
                            <textarea class='feedback-textarea' name='feedback' cols='30' rows='10' minlength='5' placeholder='请输入反馈信息'></textarea>
                            <button class='btn btn-primary sub-btn' type='submit'>提交反馈</button>
                        </form>
                    </div>
                ";
            } else {
                $row_feedback = mysqli_fetch_assoc($result_feedback);
                echo"<span>已反馈</span></div>
                    <div class='feedback-div-items'>
                        <span style='font-weight: bold;'>反馈内容：</span>
                        <br>
                        <div class='feedback-content'>". $row_feedback['content'] ."</div>
                    </div>
                ";
            }
        }
    }        
}